import java.util.Arrays;

public class Method {
    public static void main(String[] args){
        int[] arr = {1,2,3,4};
        System.out.println(find(arr,2));
        int[] array = {1,2,3,4,5,6};
        System.out.println(binarySearch(array,4));
        int[] arr1 = {2,6,5,7};
        bubbleSort(arr1);
        System.out.println(Arrays.toString(arr1));
        reverse(array);
        System.out.println(Arrays.toString(array));
        int[][] arr2 = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        //二维数组的遍历:
        for(int row = 0;row <arr2.length;row++){
            for(int col = 0;col <arr2[row].length;col++){
                System.out.printf("%d\t",arr2[row][col]);
            }
            System.out.println("");
        }
        int[] arr3 = copy(array);
        System.out.println(Arrays.toString(array));
    }
    public static int find(int[] arr,int data){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == data ){
                return i;
            }
        }
        return -1;
    }
    //二分查找
    public static int binarySearch(int[] arr,int find){
        int left = 0;
        int right = arr.length-1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(find < arr[mid]){
                right = mid - 1;
            }else if(find > arr[mid]){
                left = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    //冒泡排序：
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length-i; j++) {
                if(arr[j-1] > arr[j]){
                    int tmp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }
    //数组逆序；
    public static void reverse(int[] arr){
        int left = 0;
        int right = arr.length-1;
        while(left < right){
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }
    //数组的拷贝：
    public static int[] copy(int[] array){
        int[] copyArray = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copyArray[i] = array[i];
        }
        return copyArray;
    }
}
